<template>
    <CreatedQuestion
        :close="() => { router.push({ name: '比赛题目' }) }"
        :getstart="asyncgetquestion"
        :ID="ID"
        :asynccreateorupdatequestion="asynccreateorupdatequestion"
        :istype="true"
        :isrange="true"
        :teams="$store.getters.getteam"
        :userid="$store.getters.getuser?.ID"
    ></CreatedQuestion>
</template>

<script setup lang="ts">
import { computed, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { asynccreatedorupdatematchquestion } from "../../../api";
import CreatedQuestion from '../../../components/createdquestion/index.vue'
import Studycodemodel from "../../../mysql_interface/studycodemodel";

const router = useRouter()

async function asyncgetquestion() {
    const data = await asynccreatedorupdatematchquestion({ ID: matchquestionid.value }) as Studycodemodel.Imatchquestion

    return Object.assign<Studycodemodel.Imatchquestion, Studycodemodel.Iquestion>(data, {
        testcases: data.example
    })
}

async function asynccreateorupdatequestion(body: Studycodemodel.Iquestion) {
    const data = await asynccreatedorupdatematchquestion(Object.assign<Studycodemodel.Iquestion, Studycodemodel.Imatchquestion>(body, {
        example: body.testcases
    })) as Studycodemodel.Imatchquestion
    return Object.assign<Studycodemodel.Imatchquestion, Studycodemodel.Iquestion>(data, {
        testcases: data.example
    })
}

const route = useRoute()

const ID = computed(() => {
    return Number(route.params.ID)
})

const matchquestionid = computed(() => {
    return Number(route.query.ID)
})

watch(() => matchquestionid.value, (v, o) => {
    if (!v) {
        router.push({
            query: {
                ID: o
            }
        })
    }
})


function start() {

}

</script>